package com.my.test.http;

import android.content.Context;

import com.my.commonlib.net.okgo.DataTaskListener;
import com.my.commonlib.net.okgo.DataTaskListener1;
import com.my.commonlib.net.okgo.HttpRequestBase;
import com.my.commonlib.net.okgo.MyByteCallback;
import com.my.commonlib.net.okgo.MyStringCallback;
import com.my.commonlib.net.okgo.TaskError;
import com.my.commonlib.util.JsonUtility;
import com.my.commonlib.util.Md5;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;

import org.json.JSONObject;

import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class RequestTask extends HttpRequestBase {

    private static final String TAG = "OkHttp";
    private Context mContext;

    private String url = "";
    private TaskError vtError;
    HttpParams params;

    public RequestTask(Context context, String url) {
        super(context);
        this.mContext = context;
        this.url = url;
        params = new HttpParams();
        vtError = new TaskError();
    }

    public RequestTask(Context context) {
        super(context);
        this.mContext = context;
        params = new HttpParams();
        vtError = new TaskError();
    }

    public RequestTask setUrl(String url) {
        this.url = url;
        return this;
    }

    private void initParams(JSONObject data) {
        params.clear();
        Map<String, String> map = JsonUtility.convertJS2Obj(data.toString(), Map.class);
        params.put(map, true);
    }


    public void initPOST(JSONObject data, final DataTaskListener handler) {
        initPOST(data, false, handler);
    }

    //loading添加
    public void initPOST(JSONObject data, boolean is_show_loading, boolean isBackground, final DataTaskListener handler) {
        initPOST(data, false, handler, false, is_show_loading, isBackground);
    }

    public void initPOST(JSONObject data, final DataTaskListener1 handler, boolean flag) {
        initPOST(data, handler, false, flag);
    }

    public void initPOST(JSONObject data, boolean isNeedAll, final DataTaskListener handler) {
        initPOST(data, isNeedAll, handler, false, false, false);
    }

    //loading 添加
    public void initPOST(JSONObject data, boolean isNeedAll, boolean is_show_loading, boolean isBackground, final DataTaskListener handler) {
        initPOST(data, isNeedAll, handler, false, is_show_loading, isBackground);
    }

    public void initPOST(boolean isCache, JSONObject data, boolean isNeedAll, final DataTaskListener handler) {
        initPOST(data, isNeedAll, handler, isCache, false, false);
    }

    String msg;

    public void initPOST(JSONObject data, final boolean isNeedAll, final DataTaskListener handler,
                         final boolean iscache, final boolean is_show_loading, final boolean isBackground) {
//        NetworkManager nM = new NetworkManager();
        setShowLoadingProgress(is_show_loading);
//        if (!nM.currentNetworkIsConnected()) {
//            Logger.d("===sdfsdf=NetworkManager=" + nM.currentNetworkIsConnected());
//            vtError.setMessage("网络请求失败，请检查您的网络！");
//            handler.fail(vtError);
//            return;
//        }

        startLoadingProgress();
        initParams(data);
        OkGo.post(url)
                .tag(mContext)
                .params(params)
                .cacheMode(iscache ? CacheMode.FIRST_CACHE_THEN_REQUEST : CacheMode.NO_CACHE)
                .cacheKey(iscache ? Md5.md5(url.getBytes()) : null)
                .execute(new MyStringCallback(mContext) {
                    @Override
                    public void onSuccess(String content, Call call, Response response) {
                        try {
                            content = content.replace("\ufeff", "");

                            handler.success(isNeedAll ? content : content);

                        } catch (Exception e) {
                            e.printStackTrace();
                            vtError.setMessage("错误");
                            vtError.setContent(e.getMessage());
                            handler.fail(vtError);
                        } finally {
                            dismissLoadingprogress();
                        }
                    }

                    @Override
                    public void onCacheSuccess(String content, Call call) {
                        super.onCacheSuccess(content, call);
                        try {
                            content = content.replace("\ufeff", "");
                            handler.success(isNeedAll ? content : content);
                        } catch (Exception e) {
                            e.printStackTrace();
                            vtError.setMessage("错误");
                            vtError.setContent(e.getMessage());
                            handler.fail(vtError);
                        } finally {
                            dismissLoadingprogress();
                        }
                    }

                    @Override
                    public void onCacheError(Call call, Exception e) {
                        super.onCacheError(call, e);
                        e.printStackTrace();
                        dismissLoadingprogress();
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                        vtError.setMessage("错误");
                        vtError.setContent(e.getMessage());
                        handler.fail(vtError);
                        dismissLoadingprogress();
                    }
                });
    }


    public void initPOST(JSONObject data, final DataTaskListener1 handler, final boolean iscache, final boolean flag) {
        initParams(data);
        OkGo.post(url)
                .tag(mContext)
                .params(params)
                .execute(new MyByteCallback(mContext) {
                    @Override
                    public void onSuccess(byte[] bytes, Call call, Response response) {
                        try {
                            if (bytes != null) {
                                handler.success(bytes);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        vtError.setMessage("错误");
                        vtError.setContent(e.getMessage());
                        handler.fail(vtError);
                    }
                });
    }

    public void initGet(final DataTaskListener handler){
        OkGo.get(url)
                .tag(mContext)
                .execute(new MyStringCallback(mContext) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            if (s != null) {
                                handler.success(s);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        vtError.setMessage("错误");
                        vtError.setContent(e.getMessage());
                        handler.fail(vtError);
                    }
                });
    }

}
